#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：大路网网格化仿真
@File ：redis_utils.py
@IDE ：PyCharm
@Author ：xinyingjie
@Date ：2025/2/18 10:28
'''
import json

import redis

redisHost = '10.100.12.58'
redisPort = 6379
redisDb = 12
redisPassword = None


def send_redis_hset(k1, k2, v):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    # pool = redis.ConnectionPool(host='10.100.12.58', port=6379, db=11, decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    r.hset(k1, k2, v)

def get_redis_hset(k1,k2):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    # pool = redis.ConnectionPool(host='10.100.12.58', port=6379, db=11, decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    data = r.hget(k1, k2)
    if data is not None:
        return True
    else:
        return False
def get_redis_hset_data(k1,k2):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    # pool = redis.ConnectionPool(host='10.100.12.58', port=6379, db=11, decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    data = r.hget(k1, k2)
    if data is not None:
        return json.loads(data.decode())
    else:
        return None

def get_event_redis(key1):
    if not redisPassword:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, decode_responses=False)
    else:
        pool = redis.ConnectionPool(host=redisHost, port=redisPort, db=redisDb, password=redisPassword,
                                    decode_responses=False)
    # pool = redis.ConnectionPool(host='10.100.12.58', port=6379, db=11, decode_responses=False)
    r = redis.Redis(connection_pool=pool)
    names = r.hgetall(key1)
    result = []
    for name in names:
            value = r.hget(key1, name)
            if value:
                # print(value.decode())
                result.append(json.loads(value.decode()))

    return result
